package com.ruoyi.app.util;

import java.security.SecureRandom;
import java.util.Base64;

public class SecureRandomGenerator {
    private static final SecureRandom secureRandom = new SecureRandom();
    private static final String ALPHANUMERIC_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final String SPECIAL_CHARS = "!@#$%^&*()-_=+[]{}|;:'\",.<>/?";
    private static final String ALL_CHARS = ALPHANUMERIC_CHARS + SPECIAL_CHARS;

    /**
     * 生成指定长度的随机字母数字字符串
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String generateAlphanumericString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须为正整数");
        }
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = secureRandom.nextInt(ALPHANUMERIC_CHARS.length());
            sb.append(ALPHANUMERIC_CHARS.charAt(randomIndex));
        }
        return sb.toString();
    }

    /**
     * 生成包含特殊字符的高强度随机字符串
     * @param length 字符串长度
     * @return 高强度随机字符串
     */
    public static String generateHighStrengthString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须为正整数");
        }
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = secureRandom.nextInt(ALL_CHARS.length());
            sb.append(ALL_CHARS.charAt(randomIndex));
        }
        return sb.toString();
    }

    /**
     * 生成安全的随机字节数组
     * @param size 字节数组大小
     * @return 随机字节数组
     */
    public static byte[] generateRandomBytes(int size) {
        if (size <= 0) {
            throw new IllegalArgumentException("大小必须为正整数");
        }
        byte[] bytes = new byte[size];
        secureRandom.nextBytes(bytes);
        return bytes;
    }

    /**
     * 生成Base64编码的安全随机字符串
     * @param byteSize 原始字节数组大小
     * @return Base64编码的随机字符串
     */
    public static String generateBase64String(int byteSize) {
        if (byteSize <= 0) {
            throw new IllegalArgumentException("字节大小必须为正整数");
        }
        byte[] bytes = generateRandomBytes(byteSize);
        return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
    }

    /**
     * 生成随机UUID（不含连字符）
     * @return 随机UUID字符串
     */
    public static String generateUUID() {
        return java.util.UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 生成介于min和max之间的随机整数（包含min和max）
     * @param min 最小值
     * @param max 最大值
     * @return 随机整数
     */
    public static int generateRandomInt(int min, int max) {
        if (min > max) {
            throw new IllegalArgumentException("最小值不能大于最大值");
        }
        return secureRandom.nextInt(max - min + 1) + min;
    }

    /**
     * 主方法，演示各种随机生成方法的使用
     */
    public static void main(String[] args) {
        System.out.println("随机字母数字字符串 (长度16): " + generateAlphanumericString(16));
        System.out.println("高强度随机字符串 (长度20): " + generateHighStrengthString(20));
        System.out.println("Base64编码随机字符串 (16字节): " + generateBase64String(16));
        System.out.println("随机UUID: " + generateUUID());
        System.out.println("随机整数 (1-100): " + generateRandomInt(1, 100));
    }
}